package com.xyx.yfy_site_msg.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyx.yfy_site_msg.system.entity.YfyCharge;
import com.xyx.yfy_site_msg.system.entity.YfyUser;
import com.xyx.yfy_site_msg.system.service.YfyChargeService;
import com.xyx.yfy_site_msg.system.utils.DataGridView;
import com.xyx.yfy_site_msg.system.utils.ResultObj;
import com.xyx.yfy_site_msg.system.utils.WebUtils;
import com.xyx.yfy_site_msg.system.vo.YfyChargeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xyx
 * @since 2021-03-09
 */
@RestController
@RequestMapping("/charge")
public class YfyChargeController {

    @Autowired
    private YfyChargeService chargeService;

    /**
     * 分页查询收费记录
     *
     * @param chargeVo
     * @return
     */
    @RequestMapping("loadAllChargeForPage")
    public DataGridView loadAllChargeForPage(YfyChargeVo chargeVo) {
        Page<YfyCharge> page = new Page<>(chargeVo.getPage(), chargeVo.getLimit());
        QueryWrapper<YfyCharge> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(chargeVo.getCustomer()), "customer", chargeVo.getCustomer());
        queryWrapper.like(StringUtils.isNotBlank(chargeVo.getAddress()), "address", chargeVo.getAddress());
        queryWrapper.like(StringUtils.isNotBlank(chargeVo.getCreateBody()), "create_body", chargeVo.getCreateBody());
        queryWrapper.ge(chargeVo.getCreateStartTime() != null, "create_time", chargeVo.getCreateStartTime());
        queryWrapper.le(chargeVo.getCreateEndTime() != null, "create_time", chargeVo.getCreateEndTime());
        queryWrapper.orderByDesc("id");
        this.chargeService.page(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 添加收费记录
     *
     * @param chargeVo
     * @return
     */
    @RequestMapping("addCharge")
    public ResultObj addCharge(YfyChargeVo chargeVo) {
        YfyUser user = (YfyUser) WebUtils.getSession().getAttribute("user");
        chargeVo.setCreateBody(user.getUsername());
        chargeVo.setCreateTime(new Date());
        try {
            this.chargeService.save(chargeVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 更新收费记录
     *
     * @param chargeVo
     * @return
     */
    @RequestMapping("updateCharge")
    public ResultObj updateCharge(YfyChargeVo chargeVo) {
        try {
            this.chargeService.updateById(chargeVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除收费记录
     *
     * @param chargeVo
     * @return
     */
    @RequestMapping("deleteCharge")
    public ResultObj deleteCharge(YfyChargeVo chargeVo) {
        try {
            this.chargeService.removeById(chargeVo.getId());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @RequestMapping("batchDeleteCharge")
    public ResultObj batchDeleteCharge(Integer[] ids) {
        try {
            Collection<Serializable> list = new ArrayList<>();
            for (Integer id : ids) {
                list.add(id);
            }
            this.chargeService.removeByIds(list);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}

